package com.example.thirdhomework

import android.annotation.SuppressLint
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.util.Log
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.example.thirdhomework.databinding.ActivityThirdBinding

class ThirdActivity : AppCompatActivity() {
    private var seconds = 0
    private var running = false
    private var wasRunning = false
    private val binding by lazy { ActivityThirdBinding.inflate(layoutInflater) }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(binding.root)
        Log.d("life cycle", "onCreate")
        ViewCompat.setOnApplyWindowInsetsListener(binding.main) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
        runTimer()
        binding.buttonStart.setOnClickListener{
            running = true
        }
        binding.buttonStop.setOnClickListener{
            running = false
        }
        binding.buttonRestart.setOnClickListener{
            running = true
            seconds = 0
        }
    }

    private fun runTimer() {
        val handler = Handler(Looper.getMainLooper())
        handler.post(object : Runnable {
            override fun run() {
                val hours = seconds / 3600
                val minutes = (seconds % 3600) / 60
                val secs = seconds % 60
                binding.textView.text = String.format("%02d:%02d:%02d", hours, minutes, secs)
                if (running) {
                    seconds++
                }
                handler.postDelayed(this, 1000)
            }
        })
    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        outState.putInt("seconds", seconds)
        outState.putBoolean("running", running)
        outState.putBoolean("wasRunning", wasRunning)
        Log.d("life cycle", "onSaveInstanceState")
    }

    override fun onRestoreInstanceState(savedInstanceState: Bundle) {
        super.onRestoreInstanceState(savedInstanceState)
        seconds = savedInstanceState.getInt("seconds")
        running = savedInstanceState.getBoolean("running")
        wasRunning = savedInstanceState.getBoolean("wasRunning")
        Log.d("life cycle", "onRestoreInstanceState")
    }

    override fun onDestroy() {
        super.onDestroy()
        Log.d("life cycle", "onDestroy")
    }

    override fun onStart() {
        super.onStart()
        Log.d("life cycle", "onStart")
    }

    override fun onStop() {
        super.onStop()
        Log.d("life cycle", "onStop")
    }

    override fun onResume() {
        super.onResume()
        if (wasRunning) {
            running = true
        }
        Log.d("life cycle", "onResume")
    }

    override fun onPause() {
        super.onPause()
        wasRunning = running
        running = false
        Log.d("life cycle", "onPause")
    }
}